<template>
  <div class="type-webview">
    <el-page-header @back="goBack" content="使用内嵌登录页登陆"></el-page-header>
    <webview src="https://passport.bilibili.com/login" class="webview-page"></webview>
  </div>
</template>

<script>
const { ipcRenderer } = window.require('electron')

export default {
  name: 'LoginTypeWebview',
  data() {
    return {
      timer: 0
    }
  },
  mounted() {
    this.checkSessdata()
  },
  beforeRouteLeave(to, from, next) {
    clearInterval(this.timer)
    next()
  },
  methods: {
    checkSessdata() {
      this.timer = setInterval(() => {
        let sessdata = ipcRenderer.sendSync('check-status')
        if (sessdata) {
          this.$message({
            message: '登陆成功',
            type: 'success'
          })
          ipcRenderer.sendSync('set-sessdata', sessdata)
          this.$router.push({ name: 'Home' })
        }
      }, 200)
    },
    goBack() {
      this.$router.push({ name: 'LoginIndex' })
    }
  },
}
</script>

<style lang="scss" scoped>
.type-webview {
  display: flex;
  flex-direction: column;
  height: 100vh;
  .el-page-header {
    padding: 18px;
    border-bottom: 1px solid #dcdfe6;
  }
  .webview-page {
    flex: 1;
  }
}
</style>